package com.book.service.domain;

import com.book.dto.BookDto;
import com.book.dto.TempOrderBookDto;
import com.book.helper.OrderHelper;
import com.book.pojo.TempOrder;
import com.book.service.BookService;
import com.book.service.TempOrderService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @version 1.0
 * @Author Jincheng.Li
 * @Date 2021/6/1 10:21 上午
 * @Project_Name graduation-project
 */
@Component
@Slf4j
public class OrderCreateTempMqService {
    @Autowired
    private TempOrderService tempOrderService;
    @Autowired
    private BookService bookService;
    public void save(List<TempOrderBookDto> dtos,String orderNo){
        List<TempOrder> list = Lists.newArrayList();
        dtos.forEach(m->{
            TempOrder tempOrder = OrderHelper.covertTempDtoToTempOrderPo(m);
            tempOrder.setOrderNo(orderNo);
            list.add(tempOrder);
        });
        log.info("OrderCreateTempMqService tempOrder:{}",list);
        tempOrderService.save(list);
    }

    public void refreshBook(List<TempOrder> list){
        List<BookDto> books = Lists.newArrayList();
        try {
            list.forEach(m->{
                BookDto book = new BookDto();
                BookDto uBookByCode = bookService.getUBookByCode(m.getBookCode());
                book.setId(uBookByCode.getId());
                int num = uBookByCode.getBookNum()-m.getNum();
                if (num == 0){
                    book.setBookNum(0);
                    book.setBookFlag((short) 0);
                }else {
                    book.setBookNum(num);
                }
                books.add(book);
            });
            books.forEach(m->{
                bookService.updateSelective(m);
            });
        }catch (Exception e){
            log.error("MQ refreshBook error",e);
        }
    }

    public void cancelOrder(String orderNo) {
        List<TempOrder> tempOrderList = tempOrderService.getTempOrder(orderNo);
        if (CollectionUtils.isEmpty(tempOrderList)) {
            return;
        }
        List<BookDto> books = Lists.newArrayList();
        try {
            tempOrderList.forEach(m -> {
                BookDto book = new BookDto();
                BookDto uBookByCode = bookService.getUBookByCode(m.getBookCode());
                book.setId(uBookByCode.getId());
                int num = uBookByCode.getBookNum() + m.getNum();
                if (uBookByCode.getBookNum() == 0) {
                    book.setBookNum(num);
                    book.setBookFlag((short) 1);
                } else {
                    book.setBookNum(num);
                }
                books.add(book);
            });
            books.forEach(m -> {
                bookService.updateSelective(m);
            });
        } catch (Exception e) {
            log.error("cancelOrder resetBook error", e);
        }
    }
}
